package cases

import (
	"github.com/flipped-aurora/gin-vue-admin/server/consts"
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/cases"
	casesReq "github.com/flipped-aurora/gin-vue-admin/server/model/cases/request"
)

type CasCasesService struct{}

// CreateCasCases 创建casCases表记录
// Author [yourname](https://github.com/yourname)
func (casCasesService *CasCasesService) CreateCasCases(casCases *cases.CasCases) (err error) {
	err = global.GVA_DB.Create(casCases).Error
	return err
}

// DeleteCasCases 删除casCases表记录
// Author [yourname](https://github.com/yourname)
func (casCasesService *CasCasesService) DeleteCasCases(ID string) (err error) {
	err = global.GVA_DB.Delete(&cases.CasCases{}, "id = ?", ID).Error
	return err
}

// DeleteCasCasesByIds 批量删除casCases表记录
// Author [yourname](https://github.com/yourname)
func (casCasesService *CasCasesService) DeleteCasCasesByIds(IDs []string) (err error) {
	err = global.GVA_DB.Delete(&[]cases.CasCases{}, "id in ?", IDs).Error
	return err
}

// UpdateCasCases 更新casCases表记录
// Author [yourname](https://github.com/yourname)
func (casCasesService *CasCasesService) UpdateCasCases(casCases cases.CasCases) (err error) {
	err = global.GVA_DB.Model(&cases.CasCases{}).Where("id = ?", casCases.ID).Updates(&casCases).Error
	return err
}

// GetCasCases 根据ID获取casCases表记录
// Author [yourname](https://github.com/yourname)
func (casCasesService *CasCasesService) GetCasCases(ID string) (casCases cases.CasCases, err error) {
	err = global.GVA_DB.Where("id = ?", ID).First(&casCases).Error
	return
}

// GetCasCasesInfoList 分页获取casCases表记录
// Author [yourname](https://github.com/yourname)
func (casCasesService *CasCasesService) GetCasCasesInfoList(info casesReq.CasCasesSearch) (list []cases.CasCases, total int64, amount int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&cases.CasCases{})
	var casCasess []cases.CasCases
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.StartTouchAt != nil {
		db = db.Where("touch_at >= ?", info.StartTouchAt)
	}
	if info.EndTouchAt != nil {
		db = db.Where("touch_at <= ?", info.EndTouchAt)
	}

	if info.CaseNo != nil && *info.CaseNo != "" {
		db = db.Where("case_no = ?", *info.CaseNo)
	}
	if info.CaseName != nil && *info.CaseName != "" {
		db = db.Where("case_name LIKE ?", "%"+*info.CaseName+"%")
	}
	if info.TownCode != nil && *info.TownCode != "" {
		db = db.Where("town_code = ?", *info.TownCode)
	}
	if info.FOwnerId != nil {
		db = db.Where("f_owner_id = ?", *info.FOwnerId)
	}
	if info.CaseState != nil && *info.CaseState != "" {
		db = db.Where("case_state = ?", *info.CaseState)
	}
	if info.FCaseType != nil && *info.FCaseType != "" {
		db = db.Where("f_case_type = ?", *info.FCaseType)
	}
	if info.SCaseType != nil && *info.SCaseType != "" {
		db = db.Where("s_case_type = ?", *info.SCaseType)
	}
	if info.SOwnerId != nil {
		db = db.Where("s_owner_id = ?", *info.SOwnerId)
	}
	if info.Subdivide != nil && *info.Subdivide != "" {
		db = db.Where("subdivide = ?", *info.Subdivide)
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}

	err = db.Select("SUM(amount) as amount").Scan(&amount).Error
	if err != nil {
		return
	}

	var OrderStr string
	if _, ok := orderMap[info.Sort]; ok {
		OrderStr = info.Sort
		if info.Order == "desc" {
			OrderStr = OrderStr + " desc"
		}
		db = db.Order(OrderStr)
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Select("*").Find(&casCasess).Error
	return casCasess, total, amount, err
}
func (casCasesService *CasCasesService) GetCasCasesDataSource() (res map[string][]map[string]any, err error) {
	res = make(map[string][]map[string]any)
	polices, err := global.GetPolicesList(consts.AuthorityIdCase)
	if err != nil {
		return
	}

	res["fOwnerId"] = polices
	res["sOwnerId"] = polices
	return
}
func (casCasesService *CasCasesService) GetCasCasesPublic() {
	// 此方法为获取数据源定义的数据
	// 请自行实现
}

var orderMap = map[string]struct{}{
	"amount":   {},
	"touch_at": {},
}
